<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<title>邀请码列表</title>
		<link rel="stylesheet" href="https://cdn.nikm.cn/js/layui/css/layui.css" media="all">
	</head>
	<body class="layui-layout-body" style="overflow-y:visible;background: #fff;">
		<div class="layui-form">
		    <blockquote class="layui-elem-quote quoteBox">
		        <div class="layui-inline" style="margin-left: 2rem;">
		            <a class="layui-btn" id="create"><i class="layui-icon">&#xe608;</i> 添加邀请码</a>
		        </div>
		        <div class="layui-inline" style="margin-left: 1rem;">
		            <select id="search_status">
		                <option value="0">请选择是否使用</option>
		                <option value="1">已使用</option>
		                <option value="2">未使用</option>
		            </select>
		        </div>
		        <div class="layui-inline" style="margin-left: 1rem;">
		            <a class="layui-btn  layui-btn-normal" id="search"><i class="layui-icon ">&#xe615;</i> 搜索</a>
		        </div>
		        <div class="layui-inline" style="margin-left: 1rem;">
                	<button type="button" class="layui-btn" id="export"><i class="layui-icon ">&#xe67d;</i> 导出</button>
            	</div>
		    </blockquote>
		</div>
		<table class="layui-hide" id="table" lay-filter="table">
			
		</table>
		<div id="create_content" class="layui-form layui-form-pane" style="display: none;margin:1rem 3rem;">
		  <div class="layui-form-item">
		    <label class="layui-form-label">生成数量</label>
		    <div class="layui-input-block">
		      <input type="text" placeholder="请输入生成数量" class="layui-input" value="0" id="num">
		    </div>
		  </div>
		  <div class="layui-form-item">
		    <div class="layui-input-block">
		      <button class="layui-btn" lay-filter="formDemo" id="submit">立即提交</button>
		    </div>
		  </div>
		</div>
	</body>
	<script type="text/html" id="buttons">
		<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="accountactive">允许</a>
		<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="accountinactive">禁止</a>
	  	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>
	<script src="https://cdn.nikm.cn/js/layui/layui.js"></script>
	<script src="https://cdn.nikm.cn/js/jquery.js"></script>
	<script type="text/javascript" charset="utf-8">
		layui.use(['table','form','layer'], function(){
		  var table = layui.table;
		  var form = layui.form;
		  var layer = layui.layer;
		    table.render({
			    elem: '#table',//表格id
			    url:"./admin.php?a=invitation_code_list",//list接口地址
			    cellMinWidth: 80,//全局定义常规单元格的最小宽度
			    height: 'full-120',
			    page: true,
			    limits: [18, 50, 80, 100],
			    limit: 18,
			    cols: [[
			    //align属性是文字在列表中的位置 可选参数left center right
			    //sort属性是排序功能
			    //title是这列的标题
			    //field是取接口的字段值
			    //width是宽度，不填则自动根据值的长度
			      {field:'id', title: 'ID',align: 'center'},
			      {field:'code',title: '邀请码',align: 'center'},
			      {field:'status',title: '状态',align: 'center',templet:function(d){
			      		if(d.status == 0){
			      			return '<span style="color:green;">未使用</span>';
			      		}else{
			      			return '<span style="color:red;">已使用</span>';
			      		}
			      }},
			      {field:'email',title: '注册账号',align: 'center',templet:function(d){
			      		if(d.email){
			      			return d.email;
			      		}else{
			      			return '-';
			      		}
			      }},
				  {field:'account_status', title: '账户状态',align: 'center',templet:function(d){
				  		if(d.account_status == -1){
							return '<span style="color:red;">已禁用</span>';
						}else if(d.account_status == 0){
							return '<span style="color:green;">正常</span>';
						}else{
							return '-';
						}
				  }},
			      {field:'create_time', title: '创建时间',align: 'center'},
			      {field:'update_time', title: '最后修改时间',align: 'center'},
			      {fixed:'right',title: '操作', align:'center', toolbar: '#buttons'}
			    ]],
		  });
		   //监听
		  table.on('tool(table)', function(obj){
		      if(obj.event === 'del'){
		          layer.confirm('删除邀请码会删除关联的账户，确定删除吗？', function(index){
		              $.post("./admin.php?a=invitation_code_delete",{id:obj.data.id},function(res){
		                if (res.code == 0) {
		                    obj.del();//删除表格这行数据
		                }
		                layer.msg(res.msg);
		              },'json');
		          });
		      }
			  if(obj.event === 'accountactive'){
				  layer.confirm('允许登录？', function(index){
					  $.post("./admin.php?a=invitation_code_account_enable",{email:obj.data.email},function(res){
						  if (res.code == 1) {
							  layer.closeAll();
							  layui.use('table', function(){
								  var table = layui.table;
								  table.reload('table', { //表格的id
									  url:"./admin.php?a=invitation_code_list",
								  });
							  })
						  }
						  layer.msg(res.msg);
					  },'json');
				  });
			  }
			  if(obj.event === 'accountinactive'){
				  layer.confirm('禁止登录？', function(index){
					  $.post("./admin.php?a=invitation_code_account_disable",{email:obj.data.email},function(res){
						  if (res.code == 1) {
							  layer.closeAll();
							  layui.use('table', function(){
								  var table = layui.table;
								  table.reload('table', { //表格的id
									  url:"./admin.php?a=invitation_code_list",
								  });
							  })
						  }
						  layer.msg(res.msg);
					  },'json');
				  });
			  }
		    });
		    $('#search').click(function(){
			      //传递where条件实现搜索，并且重载表格数据
			      layui.use('table', function(){
			            var table = layui.table;
			            table.reload('table', { //表格的id
			                url:"./admin.php?a=invitation_code_list",
			                where:{
			                    'status':$('#search_status').val(),
			                }
			            });
			      })
			})
			$(document).on('keydown', function(e){
			    if(e.keyCode == 13){
			        $('#search').click();
			    }
			})
			$('#create').click(function(){
			    layer.open({
		            type: 1,
		            title:'添加邀请码',
		            skin: 'layui-layer-rim', //加上边框
		            area: ['50rem;', '12rem;'], //宽高
		            content: $('#create_content'),
		        });
			});
			$('#submit').click(function(){
			    var data = {
			        num:$('#num').val(),
			    };
			    $.post("./admin.php?a=invitation_code_create",data,function(res){
			        if (res.code == 0) {
			            layer.closeAll();
			            layui.use('table', function(){
			                var table = layui.table;
			                table.reload('table', { //表格的id
			                    url:"./admin.php?a=invitation_code_list",
			                });
			        	})
			        }
			        layer.msg(res.msg);
			    },'json');
			})
			$('#export').click(function(){
                var count = $('.layui-laypage-count').text().replace('共 ','').replace(' 条','');
                $.get("./admin.php?a=invitation_code_list"+'&page=1&limit='+count,function(res){
                    if(res.code == 0){
                        for(let k in res.data){
                            res.data[k].status == 1 ? res.data[k].status = '已使用' : res.data[k].status = '未使用';
                            res.data[k].eamil ? res.data[k].eamil = res.data[k].eamil : '-';
                        }
                        table.exportFile(['ID','邀请码','创建时间','修改时间','是否使用','注册账号'],res.data,'csv');
                    }
                },'json');
            });
		});
	</script>
</html>